package com.zytlifeservice.service.find;

import com.zytlifeservice.domain.User;
import com.zytlifeservice.domain.find.Commodity;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.repository.UserRepository;
import com.zytlifeservice.repository.find.CommodityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 积分商品业务类
 * Created by Administrator on 2017/5/31.
 */
@Service
public class CommodityService {
    @Autowired
    private CommodityRepository commodityRepository;//积分商品持久对象

    @Autowired
    private UserRepository userRepository;//用户持久对象

    //添加积分商品
    @Transactional
    public Integer addCommodity(Commodity commodity) {
//        Integer result = 0;
        Commodity one = commodityRepository.save(commodity);
//        result =
//        try {
//            commodityRepository.save(commodity);
//            result = 1;
//        } catch (Exception ex) {
//            result = 0;
//            ex.printStackTrace();
//        }
        return one == null ? 0 : 1;
    }

    //更新积分商品
    @Transactional
    public Integer updateCommodity(Commodity commodity) {
        Integer result = 0;
        Commodity one = commodityRepository.save(commodity);
//        try {
//            commodityRepository.save(commodity);
//            result = 1;
//        } catch (Exception ex) {
//            result = 0;
//            ex.printStackTrace();
//        }
        return one == null ? 0 : 1;
    }

    //查询单个积分商品
    public Commodity findOddCommodity(Long commodityId) {
        return commodityRepository.getOne(commodityId);
    }

    //删除积分商品
    @Transactional
    public Integer deleteCommodity(Long id) {
        Integer result = 0;
        try {
            commodityRepository.delete(id);
            result = 1;
        } catch (Exception ex) {
            result = 0;
            ex.printStackTrace();
        }
        return result;
    }

    //查看所有积分商品
    public List<Commodity> findAllCommodity() {
        return commodityRepository.findAllList();
    }

    //兑换商品检查
    public Integer changeGood(Long userId, Long commodityId, Integer count, Integer integral) {
        User one = userRepository.findOne(userId);
        if (one == null) {
            throw new UserException("用户不存在", 202);
        }

        int integrals = one.getUserIntegral();
        int commodityCount = findOddCommodity(commodityId).getPcount();//商品数量
        int result = 0;
        if (commodityCount >= count) {
            result = 0;
        } else {
            result = -1;
            return result;
        }
        if (integrals >= integral) {
            result = 0;
        } else {
            result = -2;
        }
        return result;
    }

    //更新商品数量
    @Transactional
    public Integer minusGoodCount(Integer pcount, Long pid) {
        Integer result = 0;
        try {
            commodityRepository.updateCount(pcount, pid);
            result = 1;
        } catch (Exception ex) {
            result = 0;
            ex.printStackTrace();
        }
        return result;
    }

}
